<?php
class userMod extends commonMod
{
	public function index()
	{
		 $sql = "SELECT * FROM {$this->model->pre}admin LEFT JOIN {$this->model->pre}group on {$this->model->pre}admin.role_id={$this->model->pre}group.id  ORDER BY {$this->model->pre}admin.uid ASC";
        $list = $this->model->query($sql); 
        $this->assign('list', $list);
		$this->display();
	}
	//添加用户
	public function add()
	{
		if(empty($_POST['do']))
		{
			$this->assign('list',$this->model->field('id,name')->table('group')->select());
			$this->display();
			return;
		}
		
		//获取数据
		$data = array();
		$data['username']= in($_POST['user']);									//帐号
		$data['password']= md5($this->config['CODE_PREFIX'].$_POST['password']);		//密码
		$data['nicename']= in($_POST['nicename']);		
		$data['role_id']= in($_POST['gid']);	
		$data['regtime']=time();						//注册时间
		$data['lock']= in($_POST['lock']);							//是否锁定
		$data['status']=1;			//默认为开启状态
		
		//数据验证
		if(empty($_POST['user']))
		{
			$this->error('请输入用户名');
		}
		if(empty($_POST['password']))
		{
			$this->error('请输入密码');
		}
		
		//添加数据
		if($this->model->table('admin')->data($data)->insert())
		{
			$this->success('添加成功！');
		}
		else
		{
			$this->error('添加失败！');
		}
	}
	//修改用户
	public function edit()
	{
		if(empty($_POST['do']))
		{
			$id = intval($_GET['0']);
			$this->assign('info',$this->info($id));
			$this->assign('list',$this->model->field('id,name')->table('group')->where($id)->select());
			$this->display();
			return;
		}
		
		$data = array();
		$data['username']		= in($_POST['user']);								//帐号
		$data['password']		= md5($this->config['CODE_PREFIX'].$_POST['password']);	//密码
		$data['role_id']	= in($_POST['gid']);							//用户组
		$data['nicename']= in($_POST['nicename']);		
		$data['regtime']	= time();						//时间
		$data['ip']	= $_SERVER["REMOTE_ADDR"];						//IP
		
		//修改数据
		$condition['uid']	= intval($_POST['id']);
		if($this->model->table('admin')->data($data)->where($condition)->update())
		{
			$this->success('更新成功！');
		}
		else
		{
			$this->error('更新失败！');
		}
	}
	
	
	//读取用户
	public function info($id)
	{
		$condition['uid'] = $id;
		$info = $this->model->table('admin')->where($condition)->find();
		if($info)
		{
			$info['group']= $this->model->table('group')->where('id='.$info['role_id'])->find();
			return $info;
		}
		else
		{
			$this->error('不存在此用户！');
		}
	}
	
	//删除用户
	public function del()
	{
		$condition['uid'] = intval($_GET['0']);
		
		 $info = $this->model->table('admin')->where($condition)->find(); 
        if($info['lock']==1){
            $this->error( '该用户已锁定,禁止删除！');
            return;
        }
		if($this->model->table('admin')->where($condition)->delete())
		{
			$this->success('删除成功！');
		}
		else
		{
			$this->error('删除失败！');
		}
	}

	//权限管理
	// public function group(){
		
	// }
}
?>